<?php
namespace Home\Controller;
use Think\Controller;

/******************会议申请控制器********************/

class TestController extends Controller {
    public function __construct(){
        parent::__construct();
        //测试数据
        $this->userid = 'WangJun';
    }

    /*
     * 本周会议  默认当天的会议室使用情况
     */
    public function this_week(){
        //获取时间
        $date = I('get.date');
        if(!$date){
            $use_date = date('Y-m-d');
            $weekarray=array("日","一","二","三","四","五","六");

            $date = $use_date.' 星期'.$weekarray[date('w')];
        }else{
            $use_date = explode('--',$date)[0];
            $date = str_replace('--',' ',$date);
        }
        //dump($date);
        //获取会议室使用情况
        $meeting_syqk = D('meeting_room');
        $data = $meeting_syqk->get_meeting_syqk_info($use_date);

        $this->assign(array(
            'data' => $data,
            'date' => $date
        ));
        //dump($data);exit;
        $this -> display();
    }


    //会议室使用情况
    public function meeting_syqk(){
        $date = I('get.date');
        $meeting_room_id = I('get.meeting_room');
        $sjd = I('get.sjd');
        $syqk = I('get.syqk');

        //取出当前会议室当天当前时间段的使用情况
        $meeting_syqk = D('meeting_syqk');
        $yi_syqk = $meeting_syqk->get_meeting_syqk_info($date,$meeting_room_id,$sjd);
        $wei_syqk = [];  //未占用的时间段
        if($syqk == '已满'){
            /************************************取出占用情况*****************************/
            //dump('已满,无未占用数据');exit;
        }else{
            /********************************算出未占用的时间***********************************/
            //获取当前会议室的数据
            $room_info = D('meeting_room')->where("id={$meeting_room_id}")->find();
            $use_time = explode(';',$room_info['use_time']);
            //计算出当前会议室当前时间段的开始和结束时间
            $_v = [];
            foreach($use_time as $K=>$v){
                if(strpos($v,$sjd) !== false){
                    $_v = explode('/',$v);
                    $_v = explode('--',$_v[1]);
                }
            }
            $start_time = strtotime($_v[0]);  //会议室可使用数据开始
            $end_time = strtotime($_v[1]);    //会议室可使用数据结束
            //dump($_v[0]);
            //dump($_v[1]);exit;
            //计算会议室的未使用时间段
            $wei_syqk = $this->get_meeting_room_weisyqk($yi_syqk,$start_time,$end_time);
            //dump($wei_syqk);exit;
            /************************************取出占用情况*****************************/
            //dump($yi_syqk);exit;

        }
        //dump($wei_syqk);exit;
        $this->assign(array(
            'id'       => $meeting_room_id,  //会议室ID
            'date'     => $date,    //日期
            'sjd'      => $sjd,    //时间段
            'yi_syqk'  => $yi_syqk ,   //已占用的时间段
            'wei_syqk' => $wei_syqk,   //未占用的时间段
        ));
        $this -> display();
    }

    /*
     * 计算会议室的未使用时间段
     */
    public function get_meeting_room_weisyqk($data,$start_time,$end_time){
        $wei_syqk = [];
        if($data){
            /*********************如果有占用数据,计算出未占用的时间段****************/
            $date_time = array_column($data,'date_time');  //已占用的时间段
            //dump($date_time);
            //把占用的时间转换成数组并按高低排序
            $time = explode('--',implode('--',$date_time));
            //dump($time);exit;
            $time_info = [];
            foreach($time as $k=>$v){
                $time_info[] = strtotime($v);
            }
            sort($time_info);

            //首先算出首尾空闲时间段,去除小于1800秒即30分钟的时间段
            $shou_time = $wei_time = '';  //首尾时间段
            if( ($time_info[0] != $start_time) && ($time_info[0]-$start_time >= 1800) ){
                $shou_time = date('H:i',$start_time).'--'.date('H:i',$time_info[0]);
            }
            if( (end($time_info) != $end_time) && ($end_time-end($time_info) >= 1800) ){
                $wei_time = date('H:i',end($time_info)).'--'.date('H:i',$end_time);
            }
            //dump($shou_time);
            //dump($wei_time);exit;
            //拼接出已经占用的时间段和中间空余的时间段 去除小于1800秒即30分钟的时间段 例如 9--10,10--10.30,10.30--11.30
            $keys = array_keys($time_info);
            $end_keys = end($keys);
            foreach($time_info as $k=>$v){
                if($k < $end_keys){
                    if( ($time_info[$k] != $time_info[$k+1]) && ($time_info[$k+1]-$time_info[$k] >= 1800) ){
                        $wei_syqk[] = date('H:i',$time_info[$k]).'--'.date('H:i',$time_info[$k+1]);
                    }
                }
            }
            //dump($wei_syqk);
            //把首尾时间段加入到未占用时间段
            if($shou_time){
                array_unshift($wei_syqk,$shou_time);
            }
            if($wei_time){
                array_push($wei_syqk,$wei_time);
            }
            //dump($wei_syqk);
            //在未占用时间段里去除已经占用的时间段
            foreach($wei_syqk as $k=>$v){
                if(in_array($v,$date_time)){
                    unset($wei_syqk[$k]);
                }
            }
        }else{
            /*********************如果没有数据,未占用时间段为此会议室的当前时间段************/
            $wei_syqk[] = date('H:i',$start_time).'--'.date('H:i',$end_time);
        }

        return $wei_syqk;
    }

    //预约会议
    public function reserve(){
        if(!IS_POST){
            $this->display('Index/reserve');
        }else{

        }

    }

    public function test(){

        $qzid = '1,4,5';

        $use_time = '2017-05-26--15:00--17:00';
        $data = D('group_member')->jcQzInfo($qzid,$use_time);
        dump($data);
    }

    //下周会议
    public function next_week(){
        $this -> display();
    }

    //我的申请
	public function apply(){
		$this -> display();
	}

	//公司会表
	public function company(){
		$this -> display();
	}

	public function info(){
		$this -> display();
	}

	public function infos(){
		$this -> display();
	}
    //我的会议
	public function my_meeting(){
		$this -> display();
	}





    //生成网页授权URL
	public function authurl(){
	    if(!IS_POST){
	        $this->display();
        }else{
            $corpid = C('CORPID');
            $app_url = C('APP_URL');
            $p = I('post.modle');
            $c = I('post.contro');
            $a = I('post.ff');
            $auth_url = $app_url.ucfirst($p).'/'.ucfirst($c).'/'.$a;
            $redirect_uri = urlencode($auth_url);
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$corpid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&agentid=126&state={$corpid}#wechat_redirect";
            echo $url;
        }
    }

    public function aa(){
        echo 11;
    }
}